class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        if (nums.size() == 0)return 0;
        sort(nums.begin(), nums.end());
        unordered_map<int, int>mp;
        int ans = 1;
        for (auto x : nums)mp[x] = 1;
        for (auto x : nums) {
            if (mp[x - 1]) {
                mp[x] = max(mp[x], mp[x - 1] + 1);
                ans = max(ans, mp[x]);
            }
        }
        return ans;
    }
};